package com.znzz.course.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.znzz.course.entity.Course;
import com.znzz.course.R;

/**
 * 添加课程表
 */
public class AddCourseActivity extends AppCompatActivity {
    //获取前端输入框的数据
    private EditText inputCourseName,inputTeacher,inputClassRoom,inputDay,inputClazzStart,inputClazzEnd,
            inputWeekStart,inputWeekEnd;
    //返回或成功提交
    private Button backButton,okButton;

    public AddCourseActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);
        setFinishOnTouchOutside(false);
        initView();

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddCourseActivity.this, MainActivity.class);
                intent.putExtra("course", "no");
                setResult(0, intent);
                finish();
            }
        });

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //课程的基本信息
                String courseName = inputCourseName.getText().toString();
                String teacher = inputTeacher.getText().toString();
                String classRoom = inputClassRoom.getText().toString();
                String day = inputDay.getText().toString();
                String clazzStart = inputClazzStart.getText().toString();
                String clazzEnd = inputClazzEnd.getText().toString();
                String weekStart = inputWeekStart.getText().toString();
                String weekEnd = inputWeekEnd.getText().toString();

                //判断数据是否为空
                if (courseName.equals("") || day.equals("") || clazzStart.equals("") || clazzEnd.equals("")|| weekStart.equals("")|| weekEnd.equals("")) {
                    Toast.makeText(AddCourseActivity.this, "基本课程信息未填写", Toast.LENGTH_SHORT).show();
                }
                else {
                    //将数据封装到course对象中
                    Course course = new Course();
                    course.setCourseName(courseName);
                    course.setTeacher(teacher);
                    course.setClassRoom(classRoom);
                    course.setDay(Integer.parseInt(day));
                    course.setClassStart(Integer.parseInt(clazzStart));
                    course.setClassEnd(Integer.parseInt(clazzEnd));
                    course.setWeekStart(Integer.parseInt(weekStart));
                    course.setWeekEnd(Integer.parseInt(weekEnd));
                    //功能跳转，传递封装的course对象
                    Intent intent = new Intent(AddCourseActivity.this, MainActivity.class);
                    intent.putExtra("course", course);
                    setResult(0, intent);
                    finish();
                }
            }
        });

    }

    //初始化控件
    public void initView(){
        inputCourseName = (EditText) findViewById(R.id.course_name);
        inputTeacher = (EditText) findViewById(R.id.teacher_name);
        inputClassRoom = (EditText) findViewById(R.id.class_room);
        inputDay = (EditText) findViewById(R.id.day);
        inputClazzStart = (EditText) findViewById(R.id.classes_begin);
        inputClazzEnd = (EditText) findViewById(R.id.classes_ends);
        inputWeekStart = findViewById(R.id.week_start);
        inputWeekEnd = findViewById(R.id.week_end);
        backButton = (Button) findViewById(R.id.back_add);
        okButton = (Button) findViewById(R.id.button);
    }
}